#include <windows.h>
#include <activscp.h>
#include "Game.h"

   class CMyScriptSite : public IActiveScriptSite, 
    public IActiveScriptSiteWindow {
   private:
      ULONG m_dwRef;
	  CGame *m_pGame;
	  HWND *m_pHwnd;
   public:
      IUnknown *m_pUnkScriptObject;
      ITypeInfo *m_pTypeInfo;

	  
      CMyScriptSite::CMyScriptSite() {
         m_pUnkScriptObject = 0;
         m_pTypeInfo = 0;
         m_dwRef = 1;

      }

	  CMyScriptSite::~CMyScriptSite() {
	  }


      // IUnknown methods...
      virtual HRESULT __stdcall QueryInterface(REFIID riid, void **ppvObject) {
         //*ppvObject = NULL;
         //return E_NOTIMPL;
		if((IID_IUnknown == riid) || (IID_IActiveScriptSite == riid))
		{
			*ppvObject = (IActiveScriptSite*)this;
			AddRef();
			return S_OK;
		}
		if(IID_IActiveScriptSiteWindow == riid)
		{

			*ppvObject = (IActiveScriptSiteWindow*)this;
			AddRef();
			return S_OK;
		} return E_NOINTERFACE;
      }

#ifdef _WIN32_WCE_CEPC
	virtual ULONG STDMETHODCALLTYPE
#else
	virtual ULONG _stdcall
#endif
      AddRef(void) {
         return ++m_dwRef;
      }
#ifdef _WIN32_WCE_CEPC
	virtual ULONG STDMETHODCALLTYPE
#else
	virtual ULONG _stdcall
#endif
	  Release(void) {
         if(--m_dwRef == 0) return 0;
         return m_dwRef;
      }

      // IActiveScriptSite methods...
      virtual HRESULT __stdcall GetLCID(LCID *plcid) {
         return S_OK;
      }
   
      virtual HRESULT __stdcall GetItemInfo(LPCOLESTR pstrName,
         DWORD dwReturnMask, IUnknown **ppunkItem, ITypeInfo **ppti) {
         // Is it expecting an ITypeInfo?
         if(ppti) {
            // Default to NULL.
            *ppti = NULL;
         
            // See if asking about ITypeInfo... 
            if(dwReturnMask & SCRIPTINFO_ITYPEINFO) {
               *ppti = m_pTypeInfo;
            }
         }
      
         // Is the engine passing an IUnknown buffer?
         if(ppunkItem) {
            // Default to NULL.
            *ppunkItem = NULL;
         
            // Is Script Engine looking for an IUnknown for our object?
            if(dwReturnMask & SCRIPTINFO_IUNKNOWN) {
               // Check for our object name...
               if (!_wcsicmp(L"Game", pstrName)) {
                  // Provide our object.
                  *ppunkItem = m_pUnkScriptObject;
                  // Addref our object...
                  m_pUnkScriptObject->AddRef();
               }
            }
         }
      
         return S_OK;
      }
   
      virtual HRESULT __stdcall GetDocVersionString(BSTR *pbstrVersion) {
         return S_OK;
      }
   
      virtual HRESULT __stdcall OnScriptTerminate(
         const VARIANT *pvarResult, const EXCEPINFO *pexcepInfo) {
         return S_OK;
      }
   
      virtual HRESULT __stdcall OnStateChange(SCRIPTSTATE ssScriptState) {
         return S_OK;
      }
   
	  virtual SetParent(CGame* lpParameter, HWND* hwnd) {
		  m_pGame = lpParameter;
		  m_pHwnd = hwnd;
		  return S_OK;
	  }


	  virtual HRESULT __stdcall OnScriptError(IActiveScriptError *pError) {
		USES_CONVERSION; 
		HRESULT hResult; 
		EXCEPINFO excep; 
		DWORD dwContext; 
		ULONG iLine; 
		LONG iChar; 


		BOOL m_bErrorOccurred=FALSE;

		hResult = pError->GetSourcePosition( &dwContext, &iLine, &iChar ); 
		if( SUCCEEDED( hResult ) ) 
		  m_bErrorOccurred = TRUE;

		hResult = pError->GetExceptionInfo( &excep ); 
		if( SUCCEEDED( hResult ) ) 
		  m_bErrorOccurred = TRUE;

		if (m_bErrorOccurred) {
			m_pGame->TimerPauseTimer(TRUE);
			m_pGame->Fire_ScriptError(iLine, iChar, excep.bstrDescription);
		}
		return S_OK;
      }
   
      virtual HRESULT __stdcall OnEnterScript(void) {
         return S_OK;
      }
   
      virtual HRESULT __stdcall OnLeaveScript(void) {
         return S_OK;
      }

#ifdef _WIN32_WCE_EMULATION
	virtual HRESULT STDMETHODCALLTYPE
#elif defined(_WIN32_WCE_CEPC)
	virtual HRESULT STDMETHODCALLTYPE
#else
	virtual HRESULT
#endif
	   GetWindow(HWND *phwnd ) {
		// set address of variable for window handle
		  
         phwnd = m_pHwnd; 

		  return S_OK;
	  }

#ifdef _WIN32_WCE_EMULATION
	virtual HRESULT STDMETHODCALLTYPE
#elif defined(_WIN32_WCE_CEPC)
	virtual HRESULT STDMETHODCALLTYPE
#else
	virtual HRESULT
#endif
 EnableModeless(BOOL fEnable) {
		  fEnable = TRUE;
		  return S_OK;
	  }
   };